<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <!-- js -->
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/layui.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <div class="form-inline">
                        <input type="text" id="aname" class="form-control input-sm" placeholder="输入文章标题...">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm" onclick="get_list()">筛选</button>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn" onclick="publish_f()">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                    <tr>
                     
                </tbody>
            </table>
            <div id="test1" class="row text-center">
            </div>
        </div>
    </div>
    <script src="./libs/art-template/template-web.js"></script>
    <script src="./js/http-myajax.js"></script>
    <!-- 定义下单列表 -->
    <script id="art_cate" type="text/html">
        {{each data}}
        <option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>
    <!-- 定义文章列表模板 -->
    <script id="art_list" type="text/html">
    {{each data}}
    <tr>
        <td>{{$value.title}}</td>
        <td>{{$value.author}}</td>
        <td>{{$value.category}}</td>
        <td class="text-center">{{$value.date}}</td>
        <td class="text-center">{{$value.state}}</td>
        <td class="text-center">
            <a href="article_edit.html?={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
            <a onclick="del({{$value.id}})" href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
        </td>
    </tr>
    {{/each}} 
    </script>
    <script>
        $(function() {
            get_pull_opt();
            get_list();
            // $('#btnSearch').click(function () {
            //     get_list();
            // }) 
        })
        function render_pager(totalcount,currentpage=1){
            // 生成分页条码控件
            layui.use('laypage', function () {
                var laypage = layui.laypage;

                //执行一个laypage实例
                laypage.render({
                    elem: 'test1'       //注意，这里的 test1 是 ID，不用加 # 号
                    , count: totalcount //数据总数，从服务端得到
                    ,curr:currentpage,
                    limit:6
                    ,jump: function (obj, first) {
                        //obj包含了当前分页的所有参数，比如：
                        // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                        // console.log(obj.limit); //得到每页显示的条数                       
                        // console.log(first);
                        //首次不执行
                        if (!first) {
                            //do something
                            get_list(obj.curr,obj.limit);
                        }
                    }
                });
            });
        }   
        // <!-- 获取文章分类数据 -->
        function get_pull_opt() {
            myAjax('get','/admin/category/list',null,function(resData) {
                let optHtml = template('art_cate',resData);
                $('#selCategory').append(optHtml);
            })
        }
        function get_list(page = 1,prepage = 6) {
            //获取参数
            let parms = {
                key:$('#aname').val(),                   //获取文章标题
                type:$('#selCategory').val(),            //文章类型
                state:$('#selStatus').val(),
                page:page,                               //获取当前分页页面 默认1
                perpage:prepage,                        //获取每页显示条数,默认6条
            }
            //分页器
            
            myAjax('get','/admin/article/query',parms,function(resData) {
                render_pager(resData.data.totalCount,page);
                let resHtml = template('art_list', resData.data);
                $('#tbody').html(resHtml);
            })
        }
        function del(a_id) {
            myAjax('POST','/admin/article/delete',{id:a_id},function(redData) {
                if(redData.code == 200) {
                    //重新调用更新
                    get_list();
                }
            })
        }
        function publish_f() {
            $(window.parent.document).find('#fub').addClass('active').siblings().removeClass('active')
        }
       
    </script>
    
</body>

</html>